package cn.itsource.org.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.itsource.basic.domain.AjaxResult;
import cn.itsource.basic.domain.BaseQuery;
import cn.itsource.basic.util.EmailUtil;
import cn.itsource.basic.util.GetUserInfoUtil;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.RedisService;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.RegisterDto;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    ShopMapper shopMapper;

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    EmailUtil emailUtil;
    @Autowired
    RedisService redisService;

    @Override
    public void insert(Shop shop) {

    }

    @Override
    public void update(Shop shop) {

    }

    @Override
    public PageList<Shop> findPage(BaseQuery baseQuery, String key) {
        return shopMapper.findPage(baseQuery, key);
    }

    @Override
    public void delete(Long id) {

    }

    @Override
    public void deleteBatch(List<Long> ids) {

    }

    @Override
    public List<Shop> findAll() {
        return shopMapper.selectAll();
    }

    @Override
    public Shop findOne(Long id) {
        return shopMapper.selectOneByExample(id);
    }

    //注册
    @Override
    public AjaxResult register(RegisterDto dto) {
        shopMapper.insertSelective(dto.getShop());

        dto.getEmployee().setShopAdmin(true);
        dto.getEmployee().setShopId(dto.getShop().getId());
        employeeMapper.insertSelective(dto.getEmployee());

        //给管理员发邮件
        /*
         *  使用uuid作为唯一标识，将数据放在redis中，并且设置时间
         * */
        String uuid = IdUtil.fastSimpleUUID();
        redisService.setStringKeyAndValue( uuid,dto.getShop(),30);
        /*
         * 使用发送邮件的工具类给注册的管理员发送邮件，并且模仿get请求将唯一标识uuid传过去
         * */
        emailUtil.send( dto.getEmployee().getEmail(),"宠物乐园商家激活",
                "尊敬"+dto.getEmployee().getUsername()+"用户您好,请在30分钟之内前往该地址完成账号激活操作:"+"http://localhost/shop/state/"+uuid);
        return AjaxResult.me();
    }

    @Override
    public AjaxResult state(String uuid) {
        if (redisService.hasKey(uuid)){
            //就需要改变商户的状态
            Shop shop = redisService.getKeyObjectValue(uuid, Shop.class);
            shop.setState(2);
            shopMapper.updateByPrimaryKeySelective(shop);
            return AjaxResult.me("当前账号已经激活,请去登录页面完成登录");
        }
        return AjaxResult.setMsg("当前链接已失效,请从新获取邮箱");
    }

    @Override
    public void upstate(Shop shop) {
        shopMapper.updateByPrimaryKeySelective(shop);
    }

    @Override
    public void back(Shop shop) {
        shopMapper.updateByPrimaryKeySelective(shop);
    }

    @Override
    public Shop findByshopId() {
        Long shopId = GetUserInfoUtil.getShopId();
        return shopMapper.findByshopId(shopId);
    }
}
